<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;
use think\Db;
use think\Exception;
use think\Log;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $request = $this->request->post(); //请求信息
        $system_id = isset($request['system_id']) ? $request['system_id'] :1;
        $where = [
            'day'=>date('Y-m-d'),
            'system_id'=>$system_id,
        ];
        $student_sign = Db::name('student_sign')->where($where)->value('id');
        if(!$student_sign){
            $student_info = Db::name('student')
                ->where(['student_status'=>1,'is_del'=>0,'trusteeship_teacher_id'=>['>',0],'school_status'=>1,'system_id'=>$system_id])
                ->select();
            $new_arr = [];
            $new_two_arr = [];
            foreach ($student_info as $w=>$v){
                if($v['trusteeship_type_id'] == 4 || $v['trusteeship_type_id'] == 5){
                    $sign_wan_one = Db::name('student_sign')->where(['sign_type'=>$v['trusteeship_type_id'],'student_id'=>$v['id'],'day'=>date('Y-m-d'),'sign_status'=>0])->find();
                    if(!$sign_wan_one){
                        $new_arr[$w]['sign_type'] = $v['trusteeship_type_id'];
                        $new_arr[$w]['student_id'] = $v['id'];
                        $new_arr[$w]['day'] = date('Y-m-d');
                        $new_arr[$w]['sign_status'] =0;
                        $new_arr[$w]['system_id'] =$system_id;
                        $new_arr[$w]['create_time'] =time();
                        $new_arr[$w]['update_time'] =time();
                    }
                }else{
                    $sign_wan = Db::name('student_sign')->where(['sign_type'=>4,'student_id'=>$v['id'],'day'=>date('Y-m-d'),'sign_status'=>0])->find();
                    if(!$sign_wan){
                        $new_arr[$w]['sign_type'] = 4;
                        $new_arr[$w]['student_id'] = $v['id'];
                        $new_arr[$w]['day'] = date('Y-m-d');
                        $new_arr[$w]['sign_status'] =0;
                        $new_arr[$w]['system_id'] =$system_id;
                        $new_arr[$w]['create_time'] =time();
                        $new_arr[$w]['update_time'] =time();
                    }
                    $sign_wan_all = Db::name('student_sign')->where(['sign_type'=>5,'student_id'=>$v['id'],'day'=>date('Y-m-d'),'sign_status'=>0])->find();
                    if(!$sign_wan_all){
                        $new_two_arr[$w]['sign_type'] = 5;
                        $new_two_arr[$w]['student_id'] =  $v['id'];
                        $new_two_arr[$w]['day']=  date('Y-m-d');
                        $new_two_arr[$w]['sign_status']  =0;
                        $new_two_arr[$w]['system_id'] =$system_id;
                        $new_two_arr[$w]['create_time'] =time();
                        $new_two_arr[$w]['update_time']  =time();
                    }

                }


            }
            shuffle($new_two_arr);
            $count = count($new_two_arr);
            if($count>0){
                $new_arr = array_merge($new_arr,$new_two_arr);
            }
            Db::name('student_sign')->insertAll($new_arr);
        }
        $this->success('获取成功');

    }

    /**
     * 首页获取当天签到情况
     *
     */
    public function get_sign_info()
    {
        $post = $this->request->post(); //请求信息
        $system_id = isset($post['system_id']) ? $post['system_id'] :1;
        $now_time = date('H:i');
        if($now_time < '14:30'){
            $sign_type = 5;
        }else{
            $sign_type = 4;
        }
        // $sign_type = isset($post['sign_type'])?$post['sign_type']:4;
        $where = [
            'day'=>date('Y-m-d'),
            'is_del'=>0,
            'student_status'=>1,
            'a.system_id'=>$system_id,
            'school_status'=>1,
            'trusteeship_teacher_id'=>['>',0],
            'sign_type'=>$sign_type,
            'class_status'=>0
        ];
        $student_info = Db::name('student')
            ->alias('a')
            ->join('student_sign s','s.student_id = a.id','left')
            ->join('my_class c','c.id =a.trusteeship_class_id')
            ->where($where)
            ->field('count(a.id) as num
            ,sum(case when s.sign_status = 0 then 1 else 0 end) as no_sign
            ,sum(case when s.sign_status = 2 then 1 else 0 end) as signd_two
            ,sum(case when s.sign_status = 1 then 1 else 0 end) as have_sign')
            ->select();
        $sql = Db::name('student')->getLastSql();
        $data = [
            'info'=>$student_info,
            'sql'=>$sql,
            'day'=>date('Y-m-d'),
            'know_url'=> Config::get('site.know_url')
        ];
        $this->success('获取成功',$data);
    }

    /**
     * 首页获取当天签到列表
     *
     */
    public function get_sign_student_list()
    {
        $post = $this->request->post(); //请求信息
        $sign_type = isset($post['sign_type'])?$post['sign_type']:4;
        $system_id = isset($post['system_id']) ? $post['system_id'] :1;
        $where = [
            'day'=>date('Y-m-d'),
            'sign_status'=>$post['sign_status'],
            'is_del'=>0,
            'student_status'=>1,
            'a.system_id'=>$system_id,
            'school_status'=>1,
            'trusteeship_teacher_id'=>['>',0],
            'sign_type'=>$sign_type
            //  'trusteeship_class_id'=>$post['class_id'],
        ];
        if(isset($post['class_id']) && $post['class_id']>0){
            $where['trusteeship_class_id'] = $post['class_id'];
        }
        $student_info = Db::name('student')
            ->alias('a')
            ->join('student_sign s','s.student_id = a.id','left')
            ->join('school_class sc','sc.id = a.school_class_id','left')
            ->join('grade g','g.id = a.grade_id','left')
            ->field('a.id,student_no,phone,student_name,sex,parents,school_class_id,image,day,sign_status,name,grade_name,remark,is_update')
            ->where($where)
            ->order('a.school_class_id asc,a.id desc')
            ->select();
        $is_notice = 0;
        $notice = '';
        foreach ($student_info as $w=>&$v){
            $v['new_title'] =$v['name'].$v['student_name'];
            $v['is_notice'] = 0;
            $v['notice'] = '';
            if($v['is_update'] == 1){
                $is_notice = 1;
                $notice = '请及时更新学生头像信息';
                $v['is_notice'] = 1;
                $v['notice'] = '请及时更新学生头像信息';
            }
        }
        $sql = Db::name('student')->getLastSql();
//        $remind_info = Db::name('remind_update')->field('frist_time,two_time')->find();
//        $now = date('m-d');

//
//
//        foreach ($student_info as $w=>&$v){
//
//            $v['is_notice'] =$is_notice;
//            $v['notice'] =$notice;
//        }

        $data = [
            'info'=>$student_info,
            'sql'=>$sql,
        ];
        $this->success('获取成功',$data);
    }

    /**
     * 首页班级签到情况
     *
     */
    public function get_my_class()
    {
        $post = $this->request->post(); //请求信息
        $system_id = isset($post['system_id']) ? $post['system_id'] :1;
        $sign_type = isset($post['sign_type'])?$post['sign_type']:4;
        $where = [
            's.day'=>date('Y-m-d'),
            'is_del'=>0,
            'sign_type'=>$sign_type,
            'a.system_id'=>$system_id,
            //  's.sign_status'=>$post['student_sign'],
        ];
        $my_class_info = Db::name('my_class')
            ->alias('a')

            ->where(['class_status'=>0,'a.system_id'=>$system_id])
            ->order('id asc')
            ->field('id,class_name')
            ->select();
        foreach ($my_class_info as $w=>&$v){
            $where['trusteeship_class_id'] = $v['id'];
            $where['student_status'] = 1;
            $where['school_status'] = 1;

            $student_info = Db::name('student')
                ->alias('a')
                ->join('student_sign s','s.student_id = a.id','left')
                ->where($where)
                ->field('count(a.id) as num
            ,sum(case when s.sign_status = 0 then 1 else 0 end) as no_sign
            ,sum(case when s.sign_status = 2 then 1 else 0 end) as signd_two
            ,sum(case when s.sign_status = 1 then 1 else 0 end) as have_sign')
                ->find();
            $v['no_sign'] = $student_info['no_sign'];
            $v['signd_two'] = $student_info['signd_two'];
            $v['have_sign'] = $student_info['have_sign'];
            //  $v['sql'] = Db::name('student')->getLastSql();

        }
        $sql = Db::name('my_class')->getLastSql();
        $data = [
            'info'=>$my_class_info,
            'sql'=>$sql,
            'day'=>date('Y-m-d'),
        ];
        $this->success('获取成功',$data);
    }

    /**
     * 签到
     *
     */
    public function set_sign()
    {
        $post = $this->request->post(); //请求信息
        try{
            $system_id = isset($post['system_id']) ? $post['system_id'] :1;
            $sign_type = isset($post['sign_type'])?$post['sign_type']:4;
            $student_id = explode(',',$post['student_id']);
            $count = count($student_id);
            if($post['sign_status'] == 3){
                $datas = [
                    'parents_id'=>$post,
                    'time'=>date('Y-m-d H:i:s'),
                ];
                Log::notice('签到状态有误'.json_encode($datas));
                $this->error('签到状态有误');
            }
            Db::startTrans();
            $where = [
                'student_id'=>['in',$student_id],
                'sign_type'=>$sign_type,
                'system_id'=>$system_id,
                'day'=>date('Y-m-d'),
            ];
            if(!isset($post['remark'])){
                $post['remark'] = '';
            }
            $update = [
                'sign_status'=>$post['sign_status'],
                'remark'=>$post['remark'],
                'update_time'=>time(),
            ];
            $res =Db::name('student_sign')->where($where)->update($update);
            $sql = Db::name('student_sign')->getLastSql();
            if($res){
                $common = new \app\api\controller\Common();
                if($post['sign_status']>0){
                    $common->send_notice($student_id,$post['sign_status']);
                }
                DB::commit();
                $this->success('签到成功',['sql'=>$sql]);
            }
            $this->error();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }


    }

    /**
     * 更新用户信息
     *
     */
    public function update_student_info()
    {
        $post = $this->request->post(); //请求信息
        $system_id = isset($post['system_id']) ? $post['system_id'] :1;
        try{
            Db::startTrans();
            $where = [
                'id'=>$post['student_id'],
                'system_id'=>$system_id,
            ];
            $update_data = [
                'image'=>$post['image'],
                'is_update'=>0,
                'update_time'=>time(),
            ];
            $res = Db::name('student')->where($where)->update($update_data);
            if($res){
                Db::commit();
                $this->success('更新成功');
            }
            $this->error();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 更新用户信息
     *
     */
    public function get_student_info()
    {
        $post = $this->request->post(); //请求信息
        $system_id = isset($post['system_id']) ? $post['system_id'] :1;
        try{
            Db::startTrans();
            $where = [
                'student_id'=>$post['student_id'],
                'system_id'=>$system_id,
            ];
            $update_data = [
                'image'=>$post['image'],
                'update_time'=>time(),
            ];
            $res = Db::name('student')->where($where)->update($update_data);
            if($res){
                Db::commit();
                $this->success('更新成功');
            }
            $this->error();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 轮播图
     *
     */
    public function get_banner_list()
    {
        $post = $this->request->post(); //请求信息
        $system_id = isset($post['system_id']) ? $post['system_id'] :1;
        $where = [
            'system_id'=>$system_id
        ];
        $banner_info = Db::name('banner')
            ->where($where)
            ->order('sort desc')
            ->select();
        $sql = Db::name('banner')->getLastSql();
        $data = [
            'info'=>$banner_info,
            'sql'=>$sql,
          //  'day'=>date('Y-m-d'),
        ];
        $this->success('获取成功',$data);
    }


}
